<?php

namespace app\admin\controller\consumer_msg;

use app\common\controller\Backstage;
use app\common\model\haikang\HkConsumerMsg;
use app\common\model\haikang\HkConsumerMsgWeek;
use app\common\service\RedisQuantity;
use hg\apidoc\annotation as Apidoc;
use thans\jwt\facade\JWTAuth;

/**
 * @Apidoc\Title("误报相关")
 */
class ConsumerMsgApply extends Backstage
{

    /**
     * @Apidoc\Title("误报列表")
     * @Apidoc\Url("/admin/consumer_msg.ConsumerMsgApply/listApi")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("keyword", type="string", desc="关键字")
     * @Apidoc\Param("keyword_time", type="array", desc="数组0开始 1结束")
     * @Apidoc\Param("page", type="int", desc="第几页默认第1页")
     * @Apidoc\Param("page_size", type="int", desc="每页几条默认10条")
     * @Apidoc\Returned("data", type="array", desc="返回说明")
     * @Apidoc\Returned("total", type="int", desc="总条数")
     */
    public function listApi()
    {
        try {
            $param = $this->request->param();
            $page_size = empty($param['page_size']) ? 10 : $param['page_size'];
            //条件
            $where = [];
            //模糊查询
            if (!empty($param['keyword'])) {
                $where[] = ['msg_name|device_name|account|remarks', 'like', "%{$param['keyword']}%"];
            }
            //时间查询
            if (!empty($param['keyword_time'])) {
                $time = 'create_time';//查询时间字段
                $timeWhere = timeWhere($param['keyword_time'][0], $param['keyword_time'][1]);
                $where[] = [$time, '>=', $timeWhere[0]];
                $where[] = [$time, '<=', $timeWhere[1] + 86399];
            }
            //排序
            $order = ['id' => 'DESC'];
            if (!empty($param['order']) && !empty($param['order_name'])) {
                $order_name = $param['order_name'];
                switch ($param['order']) {
                    case 1:
                        $order = [$order_name => 'DESC'];
                        break;
                    case 2:
                        $order = [$order_name => 'ASC'];
                        break;
                }
            }
            $list_ = \app\common\model\haikang\ConsumerMsgApply::where($where)
                ->order($order)
                ->paginate($page_size)
                ->toArray();
            $list = $list_ ['data'];
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, '成功', [
            'list' => $list,
            'total' => $list_['total'],
            'page_size' => $page_size
        ]);
    }

    /**
     * @Apidoc\Title("图片误报申请")
     * @Apidoc\Url("/admin/consumer_msg.ConsumerMsgApply/edit")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="int", desc="列表id")
     * @Apidoc\Param("hk_consumer_msg_id", type="int", desc="误报id")
     * @Apidoc\Param("yj_data_id", type="string", desc="消息回调唯一ID")
     * @Apidoc\Param("state", type="int", desc="1通过 2拒绝")
     * @Apidoc\Returned("data", type="object", desc="返回说明")
     */
    public function edit()
    {
        try {
            $param = $this->request->param();
            Validate([
                'id|id' => 'require',
                'state|审核状态' => 'require',
                'hk_consumer_msg_id|误报id' => 'require',
            ])->check($param);
            \app\common\model\haikang\ConsumerMsgApply::where(['id' => $param['id']])->update(['state' => $param['state']]);
            //通过审核删除误报
            if ($param['state'] == 1) {
                HkConsumerMsg::where(['id' => $param['hk_consumer_msg_id']])->delete();
                HkConsumerMsgWeek::where('yj_data_id', '=', function ($query) use($param) {
                    $query->name('hk_consumer_msg')->where('id', $param['hk_consumer_msg_id'])->field('yj_data_id');
                })->delete();
            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return ajaxJson(0, $e->getMessage());
        }
        return ajaxJson(1, '成功');
    }
}